using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Candy : MonoBehaviour
{
    public List<Transform> fixs = new List<Transform>();
    public List<GameObject> fragment = new List<GameObject>();
    private GameObject Crash;

    // Start is called before the first frame update
    void Start() { }

    // Update is called once per frame
    void Update() { }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Safe"))
        {
            GameManager.instance.SendMessage("turnLost");
            Debug.Log("出界");
        }

        if (collision.CompareTag("Spike"))
        {
            gameObject.SetActive(false);
            GameObject GameGo = GameManager.instance.Game;
            Crash = new GameObject("Crash");
            Crash.transform.SetParent(GameGo.transform);
            Invoke("DestroyCrash", 2.5f);
            for (int i = 0; i < fragment.Count; i++)
            {
                GameObject newFragment = Instantiate(
                    fragment[i],
                    transform.position,
                    Quaternion.identity
                );
                newFragment.transform.SetParent(Crash.transform);
                //��ը��
                Rigidbody2D rb = newFragment.GetComponent<Rigidbody2D>();
                Vector2 direction = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
                rb.AddForce(direction, ForceMode2D.Impulse);
            }
            GameManager.instance.SendMessage("turnLost");
        }
    }

    private void DestroyCrash()
    {
        Destroy(Crash);
    }
}
